package com.jinzhi.oss.controller;

import com.jinzhi.common.base.BaseController;
import com.jinzhi.common.utils.DateUtils;
import com.jinzhi.common.utils.Result;
import com.jinzhi.oss.config.MinioClientConfig;
import com.jinzhi.oss.service.SysMinioService;
import com.jinzhi.oss.utils.MinioUtil;
import io.minio.MinioClient;
import io.minio.StatObjectArgs;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletResponse;
import java.util.Date;


@RestController
@RequestMapping("/minio")
public class MinIoController extends BaseController {

    @Autowired
    private MinioUtil minioUtil;

    @Autowired
    private SysMinioService sysMinioService;

    /**
     * 上传文件
     * @param file
     * @return
     */
    @PostMapping("/uploadFile")
    @ResponseBody
    public Result<String> uploadFile(@RequestParam("file") MultipartFile file) {
        MinioClient minioClient = MinioClientConfig.getMinioClient();
        if (minioClient == null) {
            return Result.fail("连接MinIO服务器失败!");
        }
        if(file == null){
            return Result.fail("请选择要上传的文件!");
        }
        String fileName = DateUtils.format(new Date(), DateUtils.DATE_PATTERN_11) + "/" + file.getOriginalFilename();
        return minioUtil.minioUpload(file, fileName, null);
    }

    /**
     * 获取文件预览地址
     * @param fileName
     * @return
     */
    @GetMapping("/getRedFile")
    @ResponseBody
    public Result getRedFile(@RequestParam("fileName") String fileName) {
        fileName = minioUtil.getFileName(null,fileName);
        MinioClient minioClient = MinioClientConfig.getMinioClient();
        if (minioClient == null) {
            return Result.fail("连接MinIO服务器失败！");
        }
        String url = minioUtil.getPreviewFileUrl(null,fileName);
        return Result.ok(url);
    }

    /**
     * 下载文件
     * @param fileName
     * @param response
     * @return
     */
    @GetMapping("/downloadFile")
    public Result<String> downloadFile(@RequestParam("fileName") String fileName, HttpServletResponse response) {
        fileName = minioUtil.getFileName(null,fileName);
        MinioClient minioClient = MinioClientConfig.getMinioClient();
        if (minioClient == null) {
            return Result.fail("连接MinIO服务器失败!");
        }
        return minioUtil.downloadFile(null,fileName,response) != null ? Result.ok("下载成功") : Result.fail("下载失败");
    }

    /**
     * 删除文件
     *
     * @param fileName 文件路径
     * @return
     */
    @GetMapping("/deleteFile")
    @ResponseBody
    public Result<String> deleteFile(@RequestParam("fileName") String fileName) {
        fileName = minioUtil.getFileName(null,fileName);
        MinioClient minioClient = MinioClientConfig.getMinioClient();
        if (minioClient == null) {
            return Result.fail("连接MinIO服务器失败!") ;
        }
        boolean flag = minioUtil.deleteFile("data-enpower",fileName);
        return flag == true ? Result.ok("删除成功") : Result.fail("删除失败");
    }


    /**
     * 校验文件是否已经上传
     * true:已经存在相同的文件，无需在上传
     * false:反之
     * @param md5 文件md5
     * @return
     */
    @PostMapping("/register")
    @ResponseBody
    public Result<String> register(String md5) {
        return sysMinioService.register(md5);
    }


    /**
     * 切片上传
     * @param file 上传的文件
     * @param md5  文件md5值
     * @param index 当前切片名称 1、2、3、4......
     * @return
     */
    @PostMapping("/uploadChunk")
    @ResponseBody
    @Transactional
    public Result<String> uploadChunk(MultipartFile file, String md5, String index) {
        return sysMinioService.uploadChunk(file, md5, index);
    }


    /**
     * 合并切片
     * @param md5 文件md5值
     * @param fileName 文件名称
     * @return
     */
    @PostMapping("/mergeChunks")
    @ResponseBody
    @Transactional
    public Result<String> mergeChunks(String md5, String fileName) {
        return sysMinioService.mergeChunks(md5,fileName);
    }

    /** 查询文件上传进度
     * @param md5 文件md5值
     * @return
     */
    @GetMapping("/uploadSchedule")
    @ResponseBody
    public Result<String> uploadSchedule(String md5) {
        return sysMinioService.uploadSchedule(md5);
    }


}
